package club.justwill.single.Service.impl;

import club.justwill.single.Service.ModelService;
import club.justwill.single.bean.SearchModels;
import club.justwill.single.model.JsonResponse;
import club.justwill.single.model.SecurityToken;
import club.justwill.single.model.SessionState;
import club.justwill.single.persistence.dao.ModelDao;
import club.justwill.single.persistence.dao.ProjectDao;
import club.justwill.single.persistence.jpo.Model;
import club.justwill.single.persistence.jpo.Projects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;

/**
 * Created by luoyu on 1/5/16.
 */
@Service
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class ModelServiceImpl extends BaseService implements ModelService {
    @Autowired
    private ModelDao modelDao ;
    @Autowired
    private ProjectDao projectDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Throwable.class, readOnly = true)
    public List<Model> searchModelsandProjects(SearchModels smap, JsonResponse obj) {
        List<Model> modelList = modelDao.searchModels(smap.getModelIndex(),smap.getModelSize());
        for (Model model:modelList) {
            List<Projects> projectsList = projectDao.searchProjectsByModelId(model.getModelId(),smap.getProjectIndex(),smap.getProjectSize());
            model.setProjectsSet(new HashSet<Projects>(projectsList));
        }
        return modelList;
    }
}
